package ccnu.agile.config.exception;

import ccnu.agile.bean.exception.CAPDException;
import ccnu.agile.bean.response.Response;
import ccnu.agile.enums.PubError;
import ccnu.agile.utils.JSONUtils;
import ccnu.agile.utils.LogUtils;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @Description: 全局异常拦截配置类
 * @Author: JreamY
 * @Date: 2021/5/16
 **/
@ControllerAdvice
public class ExceptionAdvice {

    @ExceptionHandler(CAPDException.class)
    @ResponseBody
    public Response systemExceptionHandler(CAPDException exception) {

        LogUtils.error("系统自定义异常拦截：{}", exception.toString());
        Response response = Response.newFailure(exception.getPubCode(), exception.getPubDesc(), exception.getPrivCode());
        response.setErrorDesc(exception.getPubDesc());
        LogUtils.inter("response: CAPDEXCEPTION -> {}", JSONUtils.toJSON(response));
        return response;
    }

    @ExceptionHandler(Exception.class)
    @ResponseBody
    public Object exceptionHandler(Exception exception) {

        LogUtils.error("系统异常拦截: 异常描述 -> {}, 异常位置 -> {}", exception.toString(), exception.getStackTrace()[0]);
        Response response = Response.newFailure(PubError.RUNTIME_EXCEPTION);
        LogUtils.inter("response: EXCEPTION -> {}", JSONUtils.toJSON(response));
        return response;
    }
}
